//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include "PDBSeqRes.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#define LENGTHTEXT 100
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
PDBSeqRes::PDBSeqRes () {
  
  this->name      = new String ();
  this->records   = new TListE <SeqResRecord> ();
  this->toString  = new String ();
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
PDBSeqRes::PDBSeqRes (const PDBSeqRes& pdbSeqRes) {
  
  this->name      = new String (pdbSeqRes.name);
  this->records   = new TListE <SeqResRecord> (pdbSeqRes.records);
  this->toString  = new String ();
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
PDBSeqRes::PDBSeqRes (const PDBSeqRes* pdbSeqRes) {
  
  this->name      = new String (pdbSeqRes->name);
  this->records   = new TListE <SeqResRecord> (pdbSeqRes->records);
  this->toString  = new String ();
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
PDBSeqRes::~PDBSeqRes () {
  
  if (this->name)     delete this->name;
  if (this->records)  delete this->records;
  if (this->toString) delete this->toString;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void PDBSeqRes::Name (String* name) {
  
  if (name) this->name->In(name);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void PDBSeqRes::Records (TListE <SeqResRecord>* records) {
  
  if (records) *(this->records) = *(records);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* PDBSeqRes::Name (void) {
  
  return this->name;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
TListE <SeqResRecord>* PDBSeqRes::Records (void) {
  
  return this->records;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PDBSeqRes::ReadData (String* data) {
  
  if (!data || !data->Length()) return false;

  printf("\n%s\n", data->Out());
  
  this->CleanData();
  
  char   tmpData [data->Length() + 1];
  char*  lines = NULL;
  
  strcpy(tmpData, data->Out());
  tmpData[data->Length()] = '\0';

  lines = strtok(tmpData, "\n");
  
  while (lines) {

    SeqResRecord* newSeqResRecord = new SeqResRecord ();
    
    if (newSeqResRecord->ReadFromString(lines)) 
      this->records->Add(newSeqResRecord);
    else
      delete newSeqResRecord;
    
    lines = strtok(NULL, "\n");

  }
  
  if (!this->records->Length()) return false;
  
  return true;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool PDBSeqRes::ReadData (char* data) {
  
  if (!data || !strlen(data)) return false;
  
  String dataChar;
  dataChar.In(data);
  
  return this->ReadData(&dataChar);    

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* PDBSeqRes::ToString (void) {
  
  this->toString->In("");
  
  this->records->SetInitial();
  while (this->records->SetNext()) {
    this->toString->Add(this->records->GetCurrent()->ToString());
    this->toString->Add("\n");
  }
  
  return this->toString;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* PDBSeqRes::ToStringSequence (void) {
  
  this->toString->In("");
  
  this->records->SetInitial();
  while (this->records->SetNext()) 
    this->toString->Add(this->records->GetCurrent()->ToStringSequence());
  
  return this->toString;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void PDBSeqRes::CleanData (void) {
  
  this->name->In("");
  
  this->records->Clear();
  this->toString->In("");
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
PDBSeqRes PDBSeqRes::operator= (const PDBSeqRes& pdbSeqRes) {
  
  this->name->In(pdbSeqRes.name);
  *(this->records) = *(pdbSeqRes.records);

  return *this;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
